package com.zhongxi.guoyuan.fragment.beautifulStore;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.projectItem.ProjectDetailResult;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Created by Administrator on 2016/11/8.
 */

@SuppressLint("ValidFragment")
public class BeautifulStoreDetailFragment extends BaseFragment {

    private ImageView ivCall;
    private String id;
    private WebView wv;

    public BeautifulStoreDetailFragment(String id) {
        super();
        this.id = id;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_detail, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        wv = (WebView) getView().findViewById(R.id.wv);
        initWebView();
    }

    private void initWebView() {
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setBuiltInZoomControls(false);
        wv.getSettings().setDisplayZoomControls(false);
        wv.getSettings().setDefaultTextEncodingName("UTF-8");
        wv.getSettings().setBlockNetworkImage(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            wv.getSettings().setMixedContentMode(wv.getSettings().MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        wv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    ((WebView) view).requestDisallowInterceptTouchEvent(true);
                }else {
                    ((WebView) view).requestDisallowInterceptTouchEvent(false);
                }
                return false;
            }
        });
    }


    private String getNewContent(String htmltext) {

        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "100%").attr("height", "auto");
        }

        return doc.toString();
    }

    public void initProjectData(ProjectDetailResult result) {
        try {
            if (null != result.getExt() && !"".equals(result.getExt())) {

                if (null != result.getExt().getDescription() && !"".equals(result.getExt().getDescription())) {
                    wv.loadDataWithBaseURL(null, getNewContent(result.getExt().getDescription()), "text/html", "utf-8", null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
